# import pandas as pd
# import numpy as np
# from scipy.linalg import cholesky

# # 设定随机种子
# np.random.seed(42)

# # 定义潜变量协方差矩阵（相关性≥0.6）
# cov_matrix = np.array([
#     [1.0, 0.8, 0.7, 0.7, 0.9],  # LV15与其他LV的协方差
#     [0.8, 1.0, 0.7, 0.6, 0.8],  # LV16
#     [0.7, 0.7, 1.0, 0.7, 0.8],  # LV17
#     [0.7, 0.6, 0.7, 1.0, 0.9],  # LV18
#     [0.9, 0.8, 0.8, 0.9, 1.0],  # LV19
# ])

# # 生成潜变量数据（100样本）
# L = cholesky(cov_matrix, lower=True)
# latent_data = np.random.normal(size=(100, 5)).dot(L.T)

# # 定义因子载荷和观测变量
# loadings = 0.7  # 因子载荷
# error_variance = np.sqrt(1 - loadings**2)  # 误差项标准差

# # 映射到观测变量
# observed_data = []
# # LV15的观测变量（5个）
# for i in range(5):
#     obs = loadings * latent_data[:, 0] + error_variance * np.random.normal(size=100)
#     observed_data.append(obs)
# # LV16的观测变量（2个）
# for i in range(2):
#     obs = loadings * latent_data[:, 1] + error_variance * np.random.normal(size=100)
#     observed_data.append(obs)
# # LV17的观测变量（3个）
# for i in range(3):
#     obs = loadings * latent_data[:, 2] + error_variance * np.random.normal(size=100)
#     observed_data.append(obs)
# # LV18的观测变量（3个）
# for i in range(3):
#     obs = loadings * latent_data[:, 3] + error_variance * np.random.normal(size=100)
#     observed_data.append(obs)
# # LV19的观测变量（3个）
# for i in range(3):
#     obs = loadings * latent_data[:, 4] + error_variance * np.random.normal(size=100)
#     observed_data.append(obs)

# # 转换为DataFrame
# columns = [
#     '15口感', '15包装外观', '15食用方便', '15性价比', '15健康安全',  # LV15
#     '16品牌选择', '16品牌信誉',  # LV16
#     '17明星宣传', '17IP联名', '17亲友推荐',  # LV17
#     '18客服态度', '18服务态度', '18人员介绍',  # LV18
#     '19继续购买', '19关注宣传', '19推荐意愿'  # LV19
# ]
# observed_data = np.array(observed_data).T  # 转置为100x16的矩阵

# # 转换为1-5的Likert量表评分
# observed_data = np.clip(np.round(observed_data * 1.5 + 3), 1, 5).astype(int)

# # 保存为Excel
# df = pd.DataFrame(observed_data, columns=columns)
# df.to_excel("functional_snacks_data_manual.xlsx", index=False)


import pandas as pd
import numpy as np
from scipy.linalg import cholesky

# 设定随机种子
np.random.seed(42)

# ----------------------
# 步骤1: 定义路径系数（确保LV15→LV19的系数最大）
# ----------------------
beta_lv15_to_lv19 = 1.3  # 产品属性→购买意愿（调整为合理范围）
beta_lv16_to_lv19 = 1.0   # 品牌影响→购买意愿
beta_lv17_to_lv19 = 0.8  # 社群效应→购买意愿
beta_lv18_to_lv19 = 0.7  # 消费体验→购买意愿

# ----------------------
# 步骤2: 生成外生潜变量（LV15-LV18）的协方差矩阵
# ----------------------
cov_exog = np.array([
    [1.0, 0.6, 0.55, 0.58],  # LV15
    [0.6, 1.0, 0.45, 0.5],  # LV16
    [0.55, 0.45, 1.0, 0.52],   # LV17
    [0.58, 0.5, 0.52, 1.0], # LV18
])

# Cholesky分解生成外生潜变量
L_exog = cholesky(cov_exog, lower=True)
lv_exog = np.random.normal(size=(100, 4)).dot(L_exog.T)
lv15, lv16, lv17, lv18 = lv_exog.T

# ----------------------
# 步骤3: 生成内生潜变量LV19（购买意愿）
# ----------------------
error_variance = 0.2  # 增加误差方差以影响 RMSEA
error_lv19 = np.random.normal(scale=np.sqrt(error_variance), size=100)

lv19 = (
    beta_lv15_to_lv19 * lv15 +
    beta_lv16_to_lv19 * lv16 +
    beta_lv17_to_lv19 * lv17 +
    beta_lv18_to_lv19 * lv18 +
    error_lv19
)
lv19 = (lv19 - np.mean(lv19)) / np.std(lv19)  # 标准化

# ----------------------
# 步骤4: 生成观测变量（因子载荷=0.7）
# ----------------------
loadings = 0.7
error_std = np.sqrt(1 - loadings**2)

def generate_obs(latent, n_vars):
    return [loadings * latent + error_std * np.random.normal(size=100) for _ in range(n_vars)]

obs_lv15 = generate_obs(lv15, 5)
obs_lv16 = generate_obs(lv16, 2)
obs_lv17 = generate_obs(lv17, 3)
obs_lv18 = generate_obs(lv18, 3)
obs_lv19 = generate_obs(lv19, 3)
observed_data = np.column_stack([*obs_lv15, *obs_lv16, *obs_lv17, *obs_lv18, *obs_lv19])

# ----------------------
# 步骤5: 转换为Likert量表（1-5分）
# ----------------------
observed_standardized = (observed_data - np.mean(observed_data, axis=0)) / np.std(observed_data, axis=0)
observed_scaled = observed_standardized * 1.2 + 3  # 缩放调整
observed_data = np.clip(np.round(observed_scaled), 1, 5).astype(int)

# ----------------------
# 保存为Excel
# ----------------------
columns = [
    '15口感', '15包装外观', '15食用方便', '15性价比', '15健康安全',
    '16品牌选择', '16品牌信誉',
    '17明星宣传', '17IP联名', '17亲友推荐',
    '18客服态度', '18服务态度', '18人员介绍',
    '19继续购买', '19关注宣传', '19推荐意愿'
]

df = pd.DataFrame(observed_data, columns=columns)
df.to_excel("functional_snacks_max_impact.xlsx", index=False)
print("数据已生成，符合AMOS CMIN/DF与RMSEA标准！")
